package org.sky.util.qiniu;

import java.io.File;
import java.util.List;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.BucketManager.FileListIterator;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;

public class QiNiuUtil {
	private final static String AK="nYIAWlLcE4585b_yMkFzdTZx1T7F5MQFxQm1p7lk";
	private final static String SK="zFU_l7xlS_tav5T0dvGSMcD9WRvL3ZzqYq9DmUBT";
	private final static Auth auth;
	private final static String token;
	
	static {
		auth = Auth.create(AK,SK);
		token = auth.uploadToken("book");
	}
	
	public static void upload(File file,String fileName) throws QiniuException{
		UploadManager uploadManager = new UploadManager();
		Response res = uploadManager.put(file, fileName, token);
	}
	
	
	public static List<File> listFile(String bucket){
		BucketManager bucketManager = new BucketManager(auth);
		FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, null);
		FileInfo[] fileInfo = null ;
		if(fileListIterator.hasNext()){
			fileInfo = fileListIterator.next();
		}
		if(fileInfo != null && fileInfo.length > 0){
			for (int i = 0; i < fileInfo.length; i++) {
				System.out.println(fileInfo[i].key);
			}
		}
		
		return null;
	}
	
	
	public static void main(String[] args) throws QiniuException {
		QiNiuUtil.listFile("book");
	}
}
